package intro.databases.gtCarRental.ui;

import java.sql.Date;

import intro.databases.gtCarRental.GTCRActivity;
import intro.databases.gtCarRental.R;
import intro.databases.gtCarRental.ui.dialog.DrivingPlanDetails;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class PersonalInformation extends GTCRActivity {
	EditText firstName, middleInit, lastName, email, phoneNo, address;
	RadioGroup drivingPlans;
	RadioButton occasional, frequent, daily;
	EditText cardName, cardNo, cvv, expiryDate, billingAddress;
	
	@Override
	public void onCreate(Bundle b) {
		super.onCreate(b);
		setContentView(R.layout.personal_information);
		application.currentUser.oCreditCard = application.currentUser.creditCard;
		this.initializeFields();
		this.fillOutFields();
	}
	
	private void initializeFields() {
		firstName = (EditText) findViewById(R.id.editText_personalInformation_firstName);
		middleInit = (EditText) findViewById(R.id.editText_personalInformation_middleInitial);
		lastName = (EditText) findViewById(R.id.editText_personalInformation_lastName);
		email = (EditText) findViewById(R.id.editText_personalInformation_email);
		phoneNo = (EditText) findViewById(R.id.editText_personalInformation_phoneNumber);
		address = (EditText) findViewById(R.id.editText_personalInformation_address);
		
		drivingPlans = (RadioGroup) findViewById(R.id.radioGroup_personalInformation_drivingPlans);
		occasional = (RadioButton) findViewById(R.id.radioButton_personalInformation_occasionalDriving);
		frequent = (RadioButton) findViewById(R.id.radioButton_personalInformation_frequentDriving);
		daily = (RadioButton) findViewById(R.id.radioButton_personalInformation_dailyDriving);

		cardName = (EditText) findViewById(R.id.editText_personalInformation_nameOnCard);
		cardNo = (EditText) findViewById(R.id.editText_personalInformation_cardNumber);
		cvv = (EditText) findViewById(R.id.editText_personalInformation_cvv);
		expiryDate = (EditText) findViewById(R.id.editText_personalInformation_expiryDate);
		billingAddress = (EditText) findViewById(R.id.editText_personalInformation_billingAddress);
	}
	
	private void fillOutFields() {
		firstName.setText(application.currentUser.firstName);
		middleInit.setText(application.currentUser.middleInit);
		lastName.setText(application.currentUser.lastName);
		email.setText(application.currentUser.email);
		phoneNo.setText(application.currentUser.phoneNo);
		address.setText(application.currentUser.address);
		
		if(application.currentUser.drivingPlan.equals("Occasional Driving")) {
			occasional.setChecked(true);
		} else if(application.currentUser.drivingPlan.equals("Frequent Driving")) {
			frequent.setChecked(true);
		} else {
			daily.setChecked(true);
		}

		cardName.setText(application.currentUser.nameOnCard);
		
		if(application.currentUser.creditCard != 1)
			cardNo.setText(application.currentUser.creditCard + "");
		
		if(application.currentUser.cvv != 0)
			cvv.setText(application.currentUser.cvv + "");
		
		if(!(application.currentUser.expiryDate.toString().equals("1970-01-01")))
			expiryDate.setText(application.currentUser.expiryDate.toString());
			
		billingAddress.setText(application.currentUser.billingAddress);
	}
	
	public void updateUser() {
		application.currentUser.firstName = firstName.getText().toString();
		application.currentUser.middleInit = middleInit.getText().toString();
		application.currentUser.lastName = lastName.getText().toString();
		application.currentUser.email = email.getText().toString();
		application.currentUser.phoneNo = phoneNo.getText().toString();
		application.currentUser.address = address.getText().toString();
		
		RadioButton selected = daily;
		if(occasional.isChecked()) {
			selected = occasional;
		} else if(frequent.isChecked()) {
			selected = frequent;
		}
		application.currentUser.drivingPlan = selected.getText().toString();
		
		application.currentUser.nameOnCard = cardName.getText().toString();
		application.currentUser.creditCard = Long.parseLong(cardNo.getText().toString());
		application.currentUser.cvv = Integer.parseInt(cvv.getText().toString());
		application.currentUser.expiryDate = Date.valueOf(expiryDate.getText().toString());
		application.currentUser.billingAddress = billingAddress.getText().toString();
	}
	
	public void viewPlanDetails(View v) {
		Intent intent = new Intent(this, DrivingPlanDetails.class);
		startActivity(intent);
	}
	
	public boolean hasBlanks() {
		boolean retVal = false;
		
		if(firstName.getText().toString().equals("")) 	return true;
		if(middleInit.getText().toString().equals(""))	return true;
		if(lastName.getText().toString().equals("")) 	return true;
		if(email.getText().toString().equals(""))		return true;
		if(phoneNo.getText().toString().equals("")) 	return true;
		if(address.getText().toString().equals(""))		return true;

		if(cardName.getText().toString().equals(""))		return true;
		if(cardNo.getText().toString().equals("")) 			return true;
		if(cvv.getText().toString().equals(""))				return true;
		if(expiryDate.getText().toString().equals("")) 		return true;
		if(billingAddress.getText().toString().equals(""))	return true;
		
		return retVal;
	}
	
	@Override 
	public void onBackPressed() {
		if(hasBlanks()) {
			Toast.makeText(this, "Please fill out all fields", Toast.LENGTH_LONG).show();
		} else {
			super.onBackPressed();
		}
	}
	
	@Override 
	public void onPause() {
		super.onPause();
		updateUser();
		dbConn.updateUser(application.currentUser);
	}
}
